export default {
	location() {
		return new Promise((resolve, reject) => {
			let addressInfo = {
				longitude: '',
				latitude: '',
				ip: '',
				city: '',
				deviceInfo: '',
				querySource: 'applet',
				mapType: 0
			}
			uni.request({
				url: 'https://pv.sohu.com/cityjson?ie=utf-8',
				method: 'POST',
				success: (res) => {
					const reg = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
					let ip = reg.exec(res.data);
					var index = res.data.lastIndexOf("\:");
					var str = res.data.substring(index + 1, res.data.length);
					var reg1 = new RegExp("\"", "g")
					var str1 = str.replace(reg1, "")
					var str2 = str1.substring(0, str1.length - 2);
					addressInfo.ip = ip[0]
					uni.getSystemInfo({
						success: (res) => {
							addressInfo.deviceInfo = res.model +
								"|" +
								res.platform;
						}
					});
				}
			})
			uni.getLocation({
				type: 'wgs84',
				geocode: true,
				success: (res) => {
					console.log(res);
					let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
					let x = res.longitude;
					let y = res.latitude;
					let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
					let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
					let lngs = z * Math.cos(theta) + 0.0065;
					let lats = z * Math.sin(theta) + 0.006;
					addressInfo.longitude = lngs
					addressInfo.latitude = lats
					uni.request({
						url: 'https://m.asy315.vip/area/mapping/locate',
						// url: 'http://192.168.10.168:80/area/mapping/locate',
						method: 'GET',
						data: {
							longitude: addressInfo.longitude,
							latitude: addressInfo.latitude,
						},
						success: (data) => {
							addressInfo.city = data.data.data.province+data.data.data.city+data.data.data.district
							resolve(addressInfo)
						},
						fail: (error) => {
							reject(addressInfo)
						}
					})
				},
				fail: (error) => {
					reject(addressInfo);
				}
			});
		})
	}
}
